﻿Imports System.Text
Imports Microsoft.VisualStudio.Text.Editor
Imports System.Windows
Imports System.Windows.Input

Namespace VsSpeakerCompanion.UI
	Friend Class UIService
		Implements IUIService
		Private visualStudioService As IVisualStudioService

		''' <summary>
		''' See <see cref="IUIService"/> for more information
		''' </summary>
		''' <param name="visualStudioService"></param>
		Public Sub New(ByVal visualStudioService As IVisualStudioService)
			Me.visualStudioService = visualStudioService
		End Sub

		''' <summary>
		''' See <see cref="IUIService"/> for more information
		''' </summary>
		''' <param name="message"></param>
		Public Sub ShowMessage(ByVal message As String) Implements IUIService.ShowMessage
			MessageBox.Show(message, "Demo Dashboard Message")
		End Sub

		''' <summary>
		''' See <see cref="IUIService"/> for more information
		''' </summary>
		''' <param name="errorMessage"></param>
		Public Sub ShowError(ByVal errorMessage As String) Implements IUIService.ShowError
			Me.ShowMessage(errorMessage)
		End Sub

		''' <summary>
		''' See <see cref="IUIService"/> for more information
		''' </summary>
		''' <param name="exception"></param>
		Public Sub ShowError(ByVal exception As Exception) Implements IUIService.ShowError
			Me.ShowMessage(exception.Message)
		End Sub

		''' <summary>
		''' See <see cref="IUIService"/> for more information
		''' </summary>
		''' <param name="window"></param>
		Public Sub ShowDialog(ByVal window As Window)
			' Check if we could use the VS IUIService to show the WPF window as dialog
			window.ShowDialog()
		End Sub

		''' <summary>
		''' See <see cref="IUIService"/> for more information
		''' </summary>
		''' <param name="operation"></param>
		''' <returns></returns>
		Public Function ExecuteOperation(ByVal operation As Action) As Boolean Implements IUIService.ExecuteOperation
			Try
				Mouse.OverrideCursor = Cursors.Wait
				operation()
				Return True
			Catch ex As Exception
				Logger.Log(ex)
				ShowError(ex)
				Return False
			Finally
				Mouse.OverrideCursor = Nothing
			End Try
		End Function
	End Class
End Namespace